<template>
    <div class="login-container">
        <el-form ref="ruleFormRef" :model="subForm" :rules="formRules" class="login-form">

            <div class="title-container">
                <h3 class="title">考试后台管理系统</h3>
            </div>

            <el-form-item prop="userName">
                <span class="svg-container">
                    <svg-icon icon-class="user" />
                </span>
                <el-input ref="username" v-model="subForm.userName" placeholder="用户名" name="username" type="text" />
            </el-form-item>

            <!-- <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual> -->
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input :key="passwordType" ref="refpassword" v-model="subForm.password" :type="passwordType"
                    placeholder="密码" name="password" />
                <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                </span>
            </el-form-item>
            <!-- </el-tooltip> -->


            <el-checkbox v-model="rememberMe" style="margin-bottom: 20px;">记住密码</el-checkbox>


            <el-button :loading="subLoading" type="primary" style="width:100%;margin-bottom:30px;"
                @click.prevent="handleLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { ElMessage, type FormInstance } from 'element-plus'
import { login } from '@/api/user';
import { useRouter } from 'vue-router';

// 创建路由实例
const router = useRouter()

// 表单ref
const ruleFormRef = ref<FormInstance | undefined>()

const subForm = ref({
    userName: "admin",
    password: ""
})


const rememberMe = ref(false)

onMounted(() => {
    const saved = localStorage.getItem('loginForm');
    if (saved) {
        const { userName, password, rememberMe: savedRemember } = JSON.parse(saved);
        subForm.value.userName = userName || '';
        subForm.value.password = password || '';
        rememberMe.value = !!savedRemember;
    }
})


const formRules = ref({
    userName: [{
        required: true,
        message: '请输入用户名',
        trigger: 'blur',
    }],
    password: [{
        required: true,
        message: '请输入密码',
        trigger: 'blur',
    }]
})
const subLoading = ref(false)

const passwordType = ref<"password" | "text">("password")

function showPwd() {
    if (passwordType.value == "password") {
        passwordType.value = "text"
    } else {
        passwordType.value = "password"
    }
}

// 登录功能
async function handleLogin() {
    await ruleFormRef.value?.validate(valid => {

        if (valid) {
            subLoading.value = true
            login(subForm.value).then(res => {
                console.log(res);

                if (res.code == 1) {
                    if (rememberMe.value) {
                        localStorage.setItem('loginForm', JSON.stringify({
                            userName: subForm.value.userName,
                            password: subForm.value.password,
                            rememberMe: true
                        }));
                    } else {
                        localStorage.removeItem('loginForm');
                    }
                    ElMessage({
                        message: "登录成功",
                        type: 'success'
                    })
                    router.push('/')
                } else {
                    ElMessage({
                        message: res.message,
                        type: 'error'
                    })
                }
            }).finally(() => {
                subLoading.value = false
            })
        }
    })
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-plus css */
.login-container {
    .el-input {
        height: 47px;
        width: 85%;


        .el-input__wrapper,
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            box-shadow: none;
        }

        input {
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    height: 100vh;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        margin: 0 auto;
        overflow: hidden;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }

    .thirdparty-button {
        position: absolute;
        right: 0;
        bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
        .thirdparty-button {
            display: none;
        }
    }
}
</style>
